import tinymce from 'tinymce/tinymce'
import { openResetNumberDialog } from '@/utils/exampleTag'
import { checkBeforeInsert } from '@/utils/helpers'

tinymce.PluginManager.add('itemnote', function (editor) {
  const iconStr = `
  <svg width="24" height="25" viewBox="0 0 24 25" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <g id="页面-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <g id="编辑器页面-批注" transform="translate(-1364.000000, -161.000000)" fill="#000000" fill-rule="nonzero">
            <g id="tool" transform="translate(16.000000, 149.500000)">
                <g id="编组-2" transform="translate(190.000000, 8.000000)">
                    <g transform="translate(1158.000000, 3.500000)">
                        <rect id="矩形" opacity="0" x="0" y="0.5" width="24" height="24"></rect>
                        <path d="M20.625,3.125 L3.375,3.125 C2.96015625,3.125 2.625,3.46015625 2.625,3.875 L2.625,21.125 C2.625,21.5398437 2.96015625,21.875 3.375,21.875 L20.625,21.875 C21.0398437,21.875 21.375,21.5398437 21.375,21.125 L21.375,3.875 C21.375,3.46015625 21.0398437,3.125 20.625,3.125 Z M19.6875,20.1875 L4.3125,20.1875 L4.3125,4.8125 L19.6875,4.8125 L19.6875,20.1875 Z" id="形状"></path>
                        <polygon id="路径" points="7.640625 8.6796875 7.640625 7.4609375 16.359375 7.4609375 16.359375 8.6796875 12.9375 8.6796875 12.9375 17.5390625 11.015625 17.5390625 11.015625 8.6796875"></polygon>
                    </g>
                </g>
            </g>
        </g>
    </g>
  </svg>
`
  editor.ui.registry.addIcon('itemnoteIcon', iconStr);
  return (
    editor.ui.registry.addMenuButton('itemnote', {
      icon: 'itemnoteIcon',
      tooltip: '条文注',
      fetch: function (callback) {
        callback([
          {
            type: 'menuitem',
            text: '条文注',
            onAction: function () {
              if (!checkBeforeInsert('item_note')) return
              editor.execCommand('toggleExample', false, { action: 'item_note', dataLang: 'zh' })
            }
          },
          {
            type: 'menuitem',
            text: '条文注X',
            onAction: function () {
              if (!checkBeforeInsert('item_note')) return
              editor.execCommand('toggleExample', false, { action: 'item_note', numbered: true, dataLang: 'zh' })
            }
          },
          {
            type: 'menuitem',
            text: '重新编号',
            onAction: function () {
              openResetNumberDialog(editor, 'item_note')
            }
          }
        ])
      }
    }),
    {
      getMetadata: function () {
        return {
          name: '条文注',
          url: 'http://sfrj.cn'
        }
      }
    }
  )
})
